/**   
 * 项目名：五位一体化工安全信息平台
 * 日期：    2019-11-24 22:18:21  
 * Copyright (c) 2015- joysuch-版权所有     
 */

package com.joysuch.wwyt.risk.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.transaction.Transactional;

import com.joysuch.wwyt.core.service.BizCodeGeneratorFacade;
import com.joysuch.wwyt.risk.bean.RiskEvaluationMethodItemBean;
import com.joysuch.wwyt.risk.entity.RiskEvaluationMethodItem;
import com.joysuch.wwyt.risk.repository.RiskEvaluationMethodItemDao;
import com.joysuch.wwyt.risk.service.RiskEvaluationMethodItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class RiskEvaluationMethodItemServiceImpl implements RiskEvaluationMethodItemService {

    @Autowired
    private RiskEvaluationMethodItemDao dao;
    @Autowired
    private BizCodeGeneratorFacade bizCodeGeneratorFacade;

    @Override
    public void add(RiskEvaluationMethodItem entity) throws Exception {
        dao.save(entity);
    }

    @Override
    public void update(RiskEvaluationMethodItem entity) throws Exception {
        RiskEvaluationMethodItem content = dao.findById(entity.getId()).get();
        if (content != null) {
            // TODO set nessessary properties
            dao.save(entity);
        }
    }

    @Override
    public Page<RiskEvaluationMethodItem> findByPage(Pageable page) {
        return dao.findAll(page);
    }

    @Override
    public void delete(Long id) {
        dao.deleteById(id);
    }

    @Override
    public RiskEvaluationMethodItem findById(Long id) {
        return dao.findById(id).get();
    }

    @Override
    public List<RiskEvaluationMethodItemBean> findItems(String method, String key) {
        List<RiskEvaluationMethodItem> list = dao.findByMethodAndSubFactorCode(method, key);
        if (list.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        List<RiskEvaluationMethodItemBean> re = new ArrayList<>();
        for (RiskEvaluationMethodItem r : list) {
            RiskEvaluationMethodItemBean b = new RiskEvaluationMethodItemBean();
            b.setValue(toStr(r.getValue()));
            b.setKey(r.getSubFactorCode());
            b.setDescription(b.getValue() + " (" + r.getDescription() + ")");
            re.add(b);
        }
        return re;
    }

    private String toStr(Float value) {
        if (value == null) {
            return "0";
        }
        String a = String.valueOf(Math.round(value * 100));
        if (a.endsWith("00")) {
            return a.substring(0, a.length() - 2);
        } else if (a.endsWith("0")) {
            return a.substring(0, a.length() - 2) + "." + a.substring(a.length() - 2, a.length() - 1);
        } else {
            return a.substring(0, a.length() - 2) + "." + a.substring(a.length() - 2, a.length());
        }
    }

    @Override
    public List<RiskEvaluationMethodItemBean> findItemsByFactor(String method, String key) {
        List<RiskEvaluationMethodItem> list = dao.findByMethodAndFactor(method, key);
        if (list.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        List<RiskEvaluationMethodItemBean> re = new ArrayList<>();
        for (RiskEvaluationMethodItem r : list) {
            RiskEvaluationMethodItemBean b = new RiskEvaluationMethodItemBean();
            b.setValue(toStr(r.getValue()));
            b.setKey(r.getSubFactorCode());
            b.setDescription(b.getValue() + " (" + r.getDescription() + ")");
            re.add(b);
        }
        return re;
    }

}
